Spring Security CSRF (Cross-Site Request Forgery) আক্রমণ প্রতিরোধের জন্য একটি বিল্ট-ইন সুরক্ষা প্রদান করে। এটি প্রতিটি HTTP POST, PUT, DELETE, ইত্যাদি অনুরোধের জন্য একটি CSRF টোকেন যাচাই করে। তবে, যদি আপনাকে এই প্রক্রিয়া কাস্টমাইজ করতে হয়, যেমন Custom CSRF Token তৈরি বা Exception Handling প্রয়োগ করতে হয়, তাহলে নিম্নলিখিত ধাপগুলো অনুসরণ করা যেতে পারে।
Custom CSRF Token তৈরি
১. Custom CSRF Token Generator
Spring Security-তে ডিফল্টভাবে CsrfToken ব্যবহার করা হয়। আপনি যদি নিজস্ব কাস্টম টোকেন প্রয়োগ করতে চান, তাহলে একটি কাস্টম CsrfToken ক্লাস এবং টোকেন জেনারেটর তৈরি করতে হবে।
Custom CsrfToken ক্লাস
import org.springframework.security.web.csrf.CsrfToken;
public class CustomCsrfToken implements CsrfToken {
private final String token;
private final String parameterName;
private final String headerName;
public CustomCsrfToken(String token, String parameterName, String headerName) {
this.token = token;
this.parameterName = parameterName;
this.headerName = headerName;
}
@Override
public String getHeaderName() {
return headerName;
}
@Override
public String getParameterName() {
return parameterName;
}
@Override
public String getToken() {
return token;
}
}
Custom Token Repository
Spring Security-তে CsrfTokenRepository ইন্টারফেস ইমপ্লিমেন্ট করে একটি কাস্টম টোকেন স্টোর করতে পারেন।
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
public class CustomCsrfTokenRepository implements CsrfTokenRepository {
@Override
public CsrfToken generateToken(HttpServletRequest request) {
String token = UUID.randomUUID().toString(); // Custom token logic
return new CustomCsrfToken(token, "_csrf", "X-CSRF-TOKEN");
}
@Override
public void saveToken(CsrfToken token, HttpServletRequest request, HttpServletResponse response) {
// Save token in session or cookie
if (token == null) {
request.getSession().removeAttribute("_csrf");
} else {
request.getSession().setAttribute("_csrf", token);
}
}
@Override
public CsrfToken loadToken(HttpServletRequest request) {
return (CsrfToken) request.getSession().getAttribute("_csrf");
}
}
২. Spring Security Configuration-এ Custom CSRF যুক্ত করা
Spring Security-তে কাস্টম CsrfTokenRepository যুক্ত করতে নিচের মতো configuration ব্যবহার করুন:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf()
.csrfTokenRepository(new CustomCsrfTokenRepository()) // Custom CSRF Token
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
return http.build();
}
}
Custom CSRF Exception Handling
Spring Security CsrfException ছুঁড়ে দেয় যদি CSRF টোকেন বৈধ না হয়। আপনি কাস্টম exception handling প্রয়োগ করে এটি কাস্টমাইজ করতে পারেন।
১. Custom Access Denied Handler
AccessDeniedHandler ইন্টারফেস ইমপ্লিমেন্ট করে একটি কাস্টম handler তৈরি করুন:
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.getWriter().write("CSRF token validation failed: " + accessDeniedException.getMessage());
}
}
২. Spring Security Configuration-এ Exception Handler যুক্ত করা
Security configuration-এ কাস্টম exception handler যুক্ত করুন:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.csrf.CsrfFilter;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf()
.csrfTokenRepository(new CustomCsrfTokenRepository())
.and()
.exceptionHandling()
.accessDeniedHandler(new CustomAccessDeniedHandler()) // Custom handler
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
return http.build();
}
}
৩. Custom Exception Message বা JSON Response
JSON response প্রদান করতে CustomAccessDeniedHandler এ নিম্নলিখিত কোড যোগ করুন:
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
throws IOException, ServletException {
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.getWriter().write("{\"error\": \"CSRF token validation failed\", \"message\": \""
+ accessDeniedException.getMessage() + "\"}");
}
৪. Global Exception Handling (Optional)
Spring MVC এর মাধ্যমে Global Exception Handling প্রয়োগ করতে পারেন:
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.security.web.csrf.CsrfException;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(CsrfException.class)
public ResponseEntity<String> handleCsrfException(CsrfException ex) {
return new ResponseEntity<>("CSRF validation error: " + ex.getMessage(), HttpStatus.FORBIDDEN);
}
}
উপসংহার
Spring Security-তে Custom CSRF Token এবং Exception Handling কাস্টমাইজেশন আপনাকে নিরাপত্তা আরও শক্তিশালী এবং ব্যবহারকারীর অভিজ্ঞতা উন্নত করতে সাহায্য করে।
- Custom CSRF Token আপনার নির্দিষ্ট নিরাপত্তা প্রয়োজন মেটাতে ব্যবহার করা যেতে পারে।
- Custom Exception Handling ব্যবহারকারীদের জন্য সুনির্দিষ্ট এবং পরিষ্কার error messages প্রদান করে।
এই পদ্ধতিগুলি অনুসরণ করে আপনি Spring Security-তে উন্নত সুরক্ষা প্রয়োগ করতে পারেন।
Read more